<?php
class ProjectController extends \BaseController {

    public function __construct() {
        $this->beforeFilter('auth');
        $this->userId = Auth::user()->id;
    }

    //选片台管理页面
    public function getIndex() {
        $projects = Project::where('user_id', '=', $this->userId)->get();
        if(count($projects) != 0) {
            foreach($projects as $key => $project) {
                $pictures = DB::table('project_item')
                            ->join('pictures', 'project_item.picture_id', '=', 'pictures.id')
                            ->where('project_item.project_id', '=', $project->id)->get();

                $projects[$key]->pictures = $pictures;
            }

            return View::make('front/project_index')
                        ->with('projects', $projects);
        }

        return View::make('front/project_index')->with('projects', array());

    }

    //获取选片台列表
    public function getMyList($pictureId) {
        $projects = Project::where('user_id', '=', $this->userId)->get();
		$projectIds = DB::table('project_item')
								->select('project_id')
								->where('picture_id', '=', $pictureId)
								->get();



		foreach($projects as $key => $project) {
			$isSelect = false;
			foreach($projectIds as $projectId) {
				if($projectId->project_id == $project->id) {
					$isSelect = true;
				}
			}

			$projects[$key]->isSelect = $isSelect;
		}

        return View::make('front/project_list_form')
                        ->with('projects', $projects)
                        ->with('pictureId', $pictureId);
    }

    //选派台详情
    public function getInfo($projectId) {
        $projectInfo = Project::whereRaw('user_id = ? and id = ?', array($this->userId, $projectId))->first();

        if($projectInfo) {
            $selfSelectItems = DB::table('project_item')
                                ->join('pictures', 'project_item.picture_id', '=', 'pictures.id')
                                ->where('project_item.project_id', '=', $projectId)
								->where('project_item.user_id', '=', $this->userId)
								->get();
			$otherSelectItems = DB::table('project_item')
                                ->join('pictures', 'project_item.picture_id', '=', 'pictures.id')
                                ->where('project_item.project_id', '=', $projectId)
								->where('project_item.user_id', '!=', $this->userId)
								->get();

            return View::make('front/project_info_model')
                            ->with('projectInfo', $projectInfo)
                            ->with('selfSelectItems', $selfSelectItems)
							->with('otherSelectItems', $otherSelectItems);

        } else {
            return App::abort(404);
        }


    }

    //创建选片台
    public function postCreate() {
        $postData = Input::all();

        $project = new Project;
        $project->user_id = $this->userId;
        $project->name = $postData['name'];
        $project->description = $postData['description'];
        $project->save();

        return Redirect::to('/project')->with('message', '创建成功！');

    }

    //删除选片台
    public function getDelete($projectId) {

        $projectCount = Project::whereRaw('id = ? and user_id = ?', array($projectId, $this->userId))->count();

        if($projectCount) {
            DB::table('project_item')->where('project_id', '=', $projectId)->delete();
            Project::where('id', '=', $projectId)->delete();
            return Redirect::to('/project')->with('message', '您删除了一个项目！');
        } else {
            return App::abort(404);
        }



    }

    //从选片台删除图片
    public function postDeleteItem() {
        $projectId = Input::get('project_id');
        $itemId = Input::get('picture_id');

        $projectItemCount = DB::table('project_item')->whereRaw('project_id = ? and picture_id = ? and user_id = ?', array($projectId, $itemId, $this->userId))->count();

        if($projectItemCount) {
            DB::table('project_item')->where('project_id', '=', $projectId)->delete();

            return View::make('front/project_info')->with('message', '您成功的从选派台删除了图片！');
        }else {
            return App::abort(404);
        }



    }

    //添加图片到选片台
    public function postAddPictures() {
        $inputs = Input::all();
        $picture_id = Input::get('picture_id');
        $project_ids = Input::get('project_ids');

		if(!isset($project_ids)) {
			$project_ids = array();
		}

		$oldProjectIds = DB::table('project_item')
							->select('project_id')
							->where('picture_id', '=', $picture_id)->get();

		$tempArray = array();
		foreach($oldProjectIds as $oldProjectId) {
			array_push($tempArray, $oldProjectId->project_id);
		}

		if(!array_diff($project_ids, $tempArray) && !array_diff($tempArray, $project_ids)){
			return array('error' => '数据没有修改！');
		}

		DB::table('project_item')->where('picture_id', '=', $picture_id)->delete();



        for($i = 0; $i < count($project_ids); $i++) {
            $project_id = $project_ids[$i];
            $project = Project::find($project_id);
            if($project) {
                $projectItem = array(
                    'project_id' => $project_id,
                    'picture_id' => $picture_id,
                    'user_id' => $this->userId
                );

                DB::table('project_item')->insert($projectItem);

            }
        }

        return array('success' => '更新成功！');

    }

	public function getDownload() {
		$zip = new ZipArchiveEx();
		$zip->addDir('uploads/picture');
		$zip->close();
		return Response::download($zip);
	}

}
